package com.yue.reposense1;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLEncoder;

@WebServlet("/testresp")
public class Testresp extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //获取请求中的文件名称
        String filename = req.getParameter("filename");
        //设置文件中的中文转码格式
        String newfilename = URLEncoder.encode(filename,"utf-8");
        //设置以附件的形式响应数据
        resp.setHeader("content-disposition","attachment;filename="+newfilename);
        //获得输入流
        InputStream is = getServletContext().getResourceAsStream("sourse/" + filename);
        //获得输出流
        ServletOutputStream os = resp.getOutputStream();
        //边读边写
        byte[] bytes = new byte[1024];
        int len = 0;
        while((len = is.read(bytes)) != -1){
            os.write(bytes, 0, len);
        }

        ServletContext servletContext = getServletContext();
        String pageSize = servletContext.getInitParameter("pageSize");
        System.out.println(pageSize);
        System.out.println(111111);
        System.out.println(22222);

    }
}
